﻿namespace LeadMaster.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using LeadMaster.Models;
    using LeadMaster.DAL;
    using LeadMaster.ViewModels;
    using System.IO;

    public class TermConditionsController : Controller
    {
        private DunsmoreContext db = new DunsmoreContext();

        //
        // GET: /TermConditions/

        public ActionResult Index()
        {
            return View(db.TermConditions.ToList());
        }

        //
        // GET: /TermConditions/Details/5

        public ActionResult Details(int id = 0)
        {
            TermConditions termconditions = db.TermConditions.Find(id);
            if (termconditions == null)
            {
                return HttpNotFound();
            }
            return View(termconditions);
        }

        //
        // GET: /TermConditions/Create

        public ActionResult Create()
        {
            ViewModelTermConditions viewModel = new ViewModelTermConditions();

            return View();
        }

        //
        // POST: /TermConditions/Create

        [HttpPost]
        public ActionResult Create(ViewModelTermConditions  viewModel, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                db.TermConditions.Add(viewModel.TermConditions);
                db.SaveChanges();

                if (file.ContentLength>0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Docs/TermConditionDocs"), fileName);
                    file.SaveAs(path);
                }

                return RedirectToAction("Index");
            }

            return View(viewModel.TermConditions);
        }

        //
        // GET: /TermConditions/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TermConditions termconditions = db.TermConditions.Find(id);
            if (termconditions == null)
            {
                return HttpNotFound();
            }
            return View(termconditions);
        }

        //
        // POST: /TermConditions/Edit/5

        [HttpPost]
        public ActionResult Edit(TermConditions termconditions)
        {
            if (ModelState.IsValid)
            {
                db.Entry(termconditions).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(termconditions);
        }

        //
        // GET: /TermConditions/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TermConditions termconditions = db.TermConditions.Find(id);
            if (termconditions == null)
            {
                return HttpNotFound();
            }
            return View(termconditions);
        }

        //
        // POST: /TermConditions/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            TermConditions termconditions = db.TermConditions.Find(id);
            db.TermConditions.Remove(termconditions);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}